package com.jn.erp.wechat.common.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.wechat.common.api.entity.WxMpUser;
import com.jn.erp.wechat.common.api.mapper.WxMpUserMapper;
import com.jn.erp.wechat.common.api.service.WxMpUserService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 19:58
 */
@Service
public class WxMpUserServiceImpl extends ServiceImpl<WxMpUserMapper, WxMpUser> implements WxMpUserService {


    //查询获取数据关联或更新
    @Override
    public boolean updateBind(String openid, String mpid) {
        boolean re = false;
        QueryWrapper<WxMpUser> qw = new QueryWrapper();
        qw.eq("openid", openid);
        List<WxMpUser> list = this.list(qw);

        if (list.size() == 0) {
            WxMpUser mpUser = new WxMpUser();
            mpUser.setOpenid(openid);
            mpUser.setMpid(mpid);
            mpUser.setBindType(1);
            re = this.save(mpUser);
        } else {
            UpdateWrapper<WxMpUser> up = new UpdateWrapper();
            up.eq("openid", openid);
            up.set("mpid",mpid);
            re = this.update(up);
        }
        return re;
    }

    //员工通知数据关联或更新
    @Override
    public boolean updateBind(Long userId, String mobile, String mpid) {
        boolean re = false;
        QueryWrapper<WxMpUser> qw = new QueryWrapper();
        qw.eq("user_id", userId);
        qw.eq("mobile", mobile);
        List<WxMpUser> list = this.list(qw);

        if (list.size() == 0) {
            WxMpUser mpUser = new WxMpUser();
            mpUser.setUserId(userId);
            mpUser.setMobile(mobile);
            mpUser.setMpid(mpid);
            mpUser.setBindType(2);
            re = this.save(mpUser);
        } else {
            UpdateWrapper<WxMpUser> up = new UpdateWrapper();
            up.eq("user_id", userId);
            up.set("mobile",mobile);
            up.set("mpid",mpid);
            re = this.update(up);
        }
        return re;
    }
}
